library lib_ui;

import 'dart:async';

import 'package:flutter/material.dart';

class SearchBarEx extends StatefulWidget {
  late final TextEditingController controller;

  final bool enabled;

  final bool autofocus;

  final FocusNode? focusNode;

  final VoidCallback? onEditingComplete;

  final String? hintText;

  final Color hintTextColor;

  final TextStyle? hintStyle;

  final StreamController<String> valueStreamController = StreamController();

  final double height;

  final double borderRadius;

  final Widget prefix;

  final BoxShadow? shadow;

  final Color backgroundColor;

  SearchBarEx(
      {TextEditingController? controller,
      this.enabled = true,
      this.autofocus = false,
      this.focusNode,
      this.onEditingComplete,
      this.hintText = '请输入企业名称关键字',
      this.hintTextColor = const Color(0xFF8B8C8C),
      this.hintStyle,
      this.height = 36,
      this.borderRadius = 16,
      this.prefix = const Image(
        width: 16,
        height: 16,
        image: AssetImage('assets/images/search.png', package: 'lib_ui'),
      ),
      this.shadow = const BoxShadow(
        color: Color(0xB3E5E5E5),
        offset: Offset(0.0, 1.0),
        blurRadius: 4,
      ),
      this.backgroundColor = Colors.white,
      Key? key})
      : super(key: key) {
    this.controller = controller ?? TextEditingController();

    this.controller.addListener(() {
      if (this.controller.value.isComposingRangeValid) {
        return;
      }

      valueStreamController.add(this.controller.text);
    });
  }

  @override
  State<StatefulWidget> createState() => _SearchBarExState();
}

class _SearchBarExState extends State<SearchBarEx> {
  @override
  void initState() {
    super.initState();

    widget.controller.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.fromLTRB(13, 0, 0, 0),
      height: widget.height,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(widget.borderRadius),
        color: widget.backgroundColor,
        boxShadow: widget.shadow != null ? [widget.shadow!] : null,
      ),
      child: Row(
        children: [
          widget.prefix,
          const SizedBox(width: 8),
          Expanded(
            child: TextField(
              controller: widget.controller,
              focusNode: widget.focusNode,
              decoration: InputDecoration(
                contentPadding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                border: const OutlineInputBorder(borderSide: BorderSide.none),
                hintText: widget.hintText,
                hintStyle: widget.hintStyle ??
                    TextStyle(
                      fontSize: 14,
                      height: 1.3,
                      color: widget.hintTextColor,
                    ),
                suffixIcon: widget.controller.text.isEmpty
                    ? null
                    : IconButton(
                        iconSize: 16,
                        onPressed: _clearTextField,
                        icon: const Icon(Icons.clear),
                      ),
              ),
              textInputAction: TextInputAction.search,
              style: const TextStyle(
                fontSize: 14,
                height: 1.3,
                color: Color(0xFF111111),
              ),
              autofocus: widget.autofocus,
              onEditingComplete: widget.onEditingComplete,
              enabled: widget.enabled,
            ),
          ),
        ],
      ),
    );
  }

  void _clearTextField() {
    widget.controller.clear();
    setState(() {});
  }
}
